package controller;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import model.dao.Database;
import view.OnLineView;
import view.SearchFriendOrGroupView;

import java.sql.ResultSet;
import java.sql.SQLException;

public class SearchFriendController {
    public TextField searchTF;
    public Button searchFBtn;
    public Button searchGBtn;
    public AnchorPane result;
    public ImageView resultImg;
    public Label resultName;
    public Button resultBtn;
    public Label noResult;
    public AnchorPane searchPane;

    private boolean flag = true;

    private String friendAccount = null;
    private String groupAccount = null;
    private int group_id = -1;

    /**
     * 开始搜索好友、群
     * @param actionEvent
     */
    public void beginSearch(ActionEvent actionEvent) {
        String searchKey = searchTF.getText();
//        System.out.println("search...");
        if (!searchKey.isEmpty()) {
            System.out.println(searchKey);
            System.out.println(flag);
            Database database = new Database();
            database.connect();
            if (flag) {
                String sql1 = "select account, nickname, portrait from `user` where account = ? or nickname = ?";
                String account = null;
                String nickname = null;
                String portrait = null;
                try {
                    ResultSet resultSet1 = database.execResult(sql1, searchKey, searchKey);
                    if (resultSet1.next()) {
                        account = resultSet1.getString("account");
                        nickname = resultSet1.getString("nickname");
                        portrait = resultSet1.getString("portrait");
                    }
                    resultSet1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println(account + "---" + nickname + "---" + portrait);
                if (account != null) {

                    friendAccount = account;

                    result.setVisible(true);
                    noResult.setVisible(false);
                    resultName.setText(nickname + "(" + account + ")");
                    resultImg.setStyle("-fx-image: url(" + portrait + ");");
                    resultBtn.setText("加好友");
                } else {
                    noResult.setVisible(true);
                    result.setVisible(false);
                }
            } else {
                String sql2 = "select group_id, group_account, group_name, group_portrait from `group` where group_account = ? or group_name = ?";
                String group_account = null;
                String group_name = null;
                String group_portrait = null;
                try {
                    ResultSet resultSet2 = database.execResult(sql2, searchKey, searchKey);
                    if (resultSet2.next()) {
                        group_id = resultSet2.getInt("group_id");
                        group_account = resultSet2.getString("group_account");
                        group_name = resultSet2.getString("group_name");
                        group_portrait = resultSet2.getString("group_portrait");
                    }
                    resultSet2.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (group_account != null) {

                    groupAccount = group_account;

                    result.setVisible(true);
                    noResult.setVisible(false);
                    resultName.setText(group_name + "(" + group_account + ")");
                    resultImg.setStyle("-fx-image: url(" + group_portrait + ");");
                    resultBtn.setText("加群");
                } else {
                    noResult.setVisible(true);
                    result.setVisible(false);
                }
            }
        }
    }

    public void addFriend(ActionEvent actionEvent) {

        SearchFriendOrGroupView view = (SearchFriendOrGroupView) searchPane.getScene().getWindow();
        OnLineView onLineView = view.getOnLineView();

        if (resultBtn.getText().equals("加好友")) {
            if (friendAccount != null) {
                onLineView.addNewFriend(friendAccount);
            }
        }
        if (resultBtn.getText().equals("加群")) {
            if (groupAccount != null && group_id != -1) {
                onLineView.addNewGroup(group_id);
            }
        }
    }

    public void searchFriend(ActionEvent actionEvent) {
        searchFBtn.setStyle("-fx-text-fill: #5a94ff");
        searchGBtn.setStyle("-fx-text-fill: grey");
        flag = true;
    }

    public void searchGroup(ActionEvent actionEvent) {
        searchFBtn.setStyle("-fx-text-fill: grey");
        searchGBtn.setStyle("-fx-text-fill: #5a94ff");
        flag = false;
    }
}
